﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MadLibrary.Net.Client
{
    /// <summary>
    /// Communicates with target by Requester.
    /// </summary>
    public class Communicator
    {
        /// <summary>
        /// Gets or sets requester for communication.
        /// </summary>
        public Requester Requester { get; set; }
        /// <summary>
        /// Gets or sets request data.
        /// </summary>
        public Request RequestData { get; set; }
        /// <summary>
        /// Gets or sets response data.
        /// </summary>
        public Response ResponseData { get; set; }
        /// <summary>
        /// Creates new empty Communicator with default values.
        /// </summary>
        public Communicator()
        {
            this.Requester = new Requester();
            this.RequestData = new Request();
            this.ResponseData = new Response();
        }
        /// <summary>
        /// Creates new communicator with requester specified.
        /// </summary>
        /// <param name="req">Specifies requester of communicator.</param>
        public Communicator(Requester req)
        {
            this.Requester = req;
        }
        /// <summary>
        /// Creates fully associated communicator object with specified requester, request and response.
        /// </summary>
        /// <param name="req">Specifies requester of communicator.</param>
        /// <param name="rqd">Specifies request of communciator.</param>
        /// <param name="rsd">Specifies response of communicator.</param>
        public Communicator(Requester req, Request rqd, Response rsd)
        {
            this.Requester = req;
            this.RequestData = rqd;
            this.ResponseData = rsd;
        }
        /// <summary>
        /// Opens connection to target.
        /// </summary>
        public virtual void Open()
        {
            this.Requester.Connect();
        }
        /// <summary>
        /// Sends request data and receives response.
        /// </summary>
        public virtual void Request()
        {
            this.Requester.Send(this.RequestData);
            this.ResponseData = this.Requester.Receive(1024);
        }
        /// <summary>
        /// Disconnects communicator.
        /// </summary>
        public virtual void Close()
        {
            this.Requester.Disconnect();
        }
    }
}
